package com.nervenets.shopping.controller.user.params;

import com.alibaba.fastjson.JSONObject;
import com.nervenets.general.utils.JodaUtils;
import com.nervenets.general.utils.StringUtils;
import com.nervenets.general.web.params.EntityPagingParams;
import com.nervenets.shopping.hibernate.domain.user.User;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;

import javax.persistence.criteria.*;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class UsersPagingParams extends EntityPagingParams<User> {
    @ApiModelProperty(value = "用户ID，不填不筛选")
    private long targetUserId;
    @ApiModelProperty(value = "邀请用户ID，不填不筛选")
    private long inviteUserId;
    @ApiModelProperty(value = "显示所有拼友")
    private boolean showAllTree;
    @ApiModelProperty(value = "显示今日在线")
    private boolean showTodayOnline;
    @ApiModelProperty(value = "用户昵称，不填不筛选")
    private String name;
    @ApiModelProperty(value = "用户电话，不填不筛选")
    private String mobile;
    @ApiModelProperty(value = "附加内容，JSON对象，key为字段，value为值，不填不筛选")
    private JSONObject extra;
    @ApiModelProperty(value = "关键词，不填不筛选")
    private String keyword;
    @ApiModelProperty(value = "是否为团长：0全部、1是、2否")
    private int inviter;
    @ApiModelProperty(value = "是否为优秀团长：0全部、1是、2否")
    private int excellentInviter;
    @ApiModelProperty(value = "是否启用：0全部、1是、2否")
    private int enable;

    @Override
    public void generateSpecification(List<Predicate> predicates, Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        addEqualsPredicate(root.get("id"), targetUserId, targetUserId > 0);
        if (inviteUserId > 0) {
            if (showAllTree) {
                predicates.add(
                        builder.or(
                                builder.equal(root.get("parentId"), inviteUserId),
                                builder.isTrue(this.builder.function("JSON_CONTAINS", Boolean.class, root.get("parentIds"), builder.literal(String.valueOf(inviteUserId))))
                        )
                );
            } else {
                addEqualsPredicate(root.get("parentId"), inviteUserId, true);
            }
        }
        addOrLikePredicate(name, root.get("name"));
        addOrLikePredicate(mobile, root.get("mobile"));
        if (null != getExtra()) {
            getExtra().forEach((key, value) -> {
                if (!StringUtils.isEmpty(key) && null != value) {
                    final Join<Object, Object> extras = root.join("extras");
                    predicates.add(extras.on(
                            builder.equal(extras.get("field"), key),
                            builder.equal(extras.get("value"), value)
                    ).getOn());
                }
            });
        }
        addOrLikePredicate(keyword, root.get("name"), root.get("mobile"));
        addYesOrNoPredicate(root.get("inviter"), inviter, 1, 2);
        addYesOrNoPredicate(root.get("excellentInviter"), excellentInviter, 1, 2);
        addYesOrNoPredicate(root.get("enable"), enable, 1, 2);
        addGreaterThanPredicate(root.get("lastLoginTime"), JodaUtils.getStartOfToday(), true, showTodayOnline);
    }
}
